<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Iframe</title>
</head>
<body>
  <iframe id="iframe" src="http://localhost:8081/index.html" title="index"></iframe>
  <button onclick="sendMsg()">通讯</button>

  <script>
    window.addEventListener("message", (event) => {
      if (event.origin == "http://localhost:8081") {
        console.group("iframe")
        console.log(event)
        console.groupEnd()
      }
    });

    let iframeWindow = document.getElementById("iframe").contentWindow;

    function sendMsg () {
      iframeWindow.postMessage("A secret", "http://localhost:8081");
    }

    // window.onload = sendMsg
  </script>
</body>
</html>